package com.yanqu.road.dao.impl.activity.zhounianturntable;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.zhounianturntable.ZhouNianTurntableRecord;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.*;
import java.util.*;

public class ZhouNianTurntableRecordDaoImpl extends TempDataObjectDao<ZhouNianTurntableRecord> {


    public static String getZhouNianTurntableRecordSql = "select * from `t_c_activity_zhounian_turntable_record` where `activityId`=? order by `time` asc limit ?";

    public List<ZhouNianTurntableRecord> getZhouNianTurntableRecordList(int activityId, int limit) {
        List<ZhouNianTurntableRecord> result = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getZhouNianTurntableRecordSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, limit);
            rs = call.executeQuery();
            while (rs.next()) {
                ZhouNianTurntableRecord data = parse(rs);
                result.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }


    public static String insertZhouNianTurntableRecordSql = "insert into `t_c_activity_zhounian_turntable_record`(activityId,userId,userBaseInfo,reward,time) values (?,?,?,?,?)";
    @Override
    protected boolean addData(ZhouNianTurntableRecord data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getReward()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTime()));
        if (getDb().executeNoneQuery(insertZhouNianTurntableRecordSql, para) > 0) {
            return true;
        }
        return false;
    }


    public static String updateZhouNianTurntableRecordSql = "update `t_c_activity_zhounian_turntable_record` set userId=?,userBaseInfo=?,reward=?,time=? where activityId=?";
    @Override
    protected boolean updateData(ZhouNianTurntableRecord data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getReward()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        if (getDb().executeNoneQuery(updateZhouNianTurntableRecordSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected ZhouNianTurntableRecord parseData(ResultSet rs) throws SQLException {
        ZhouNianTurntableRecord data = new ZhouNianTurntableRecord();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setUserBaseInfo(JSON.parseObject(rs.getString("userBaseInfo"), new TypeReference<UserBaseInfo>() {
        }));
        data.setReward(rs.getString("reward"));
        data.setTime(rs.getLong("time"));
        return data;
    }
}
